package com.bookcity.front.webfront.ctrl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bookcity.front.webfront.config.MyStore;
import com.bookcity.front.webfront.domain.Member;
import com.bookcity.front.webfront.service.IMemberService;
import com.bookcity.front.webfront.vo.Login;
import com.bookcity.front.webfront.vo.LoginResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("member")
public class MemController {

    //@Autowired
    //private HttpServletRequest req;
    @Autowired
    private IMemberService memberService;

    @Value("${douyin.appid}")
    private String appid;
    @Value("${douyin.secret}")
    private String secret;

    @Autowired
    private MyStore redisTemplate;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody Login login) throws IOException {
//        //授权时，客户端发送了code
        System.out.println(appid + ",,,,,,,,,,,,,,,,\n sec:" + secret);
       System.out.println("code:"+login.getCode());
//        login.setAppid(appid);
//        login.setSecret(secret);
        login.setAppid("ttdb04adb944386a1b01");
        login.setSecret("c48c2a88003426b36710f6e00fe8c79b743059ff");
        LoginResult login1 = Douyin.login(login);
        if (login1.getErr_no() == 0) {
            String openid = login1.getData().getOpenid();
            Member member = memberService.checkMember(openid);
            String token = String.valueOf(UUID.fastUUID());
            redisTemplate.set(token, member);
            return AjaxResult.success(token);
        }
        return AjaxResult.error("登录失败：" + login1.getErr_no() + "," + login1.getErr_tips());
//        //tt.login()
//        CloseableHttpClient client = HttpClients.createDefault();//方式二
//        HttpPost httpGet = new HttpPost("https://developer.toutiao.com/api/apps/v2/jscode2session");
//        httpGet.setHeader("Content-type","application/json");
//        httpGet.setEntity(new StringEntity(JSON.toJSONString(login)));
//        CloseableHttpResponse response = client.execute(httpGet);
//        String result = EntityUtils.toString(response.getEntity(), "utf-8");
//        JSONObject jsonObject = JSON.parseObject(result);
//        Object res = jsonObject.get("err_no");
//        //JSONObject.parse();
//        if("0".equals(res)){
//            JSONObject data = (JSONObject)jsonObject.get("data");
//            String openid =(String) data.get("openid");
//            //》》》
//            String session_key =(String) data.get("session_key");
//            Member member = memberService.checkMember(openid);
//            //redis
//            //key session_key,member
//            return AjaxResult.success("登录成功",member);
//        }
        // return AjaxResult.error("登录失败！");
    }
}
